

class PlanItemController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        //TODO:리스트 화면, 수정화면에서 생성 없애기
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ planItemInstanceList: PlanItem.list( params ), planItemInstanceTotal: PlanItem.count() ]
    }
    
    def show = {
        /*
         원래대로라면 할 일:
         로그인  팅겨내기
         자신의 계획의 planItem아니면 팅겨내기

        여기서 수정버튼 누르면 수정되어야 함
        이 말인 즉슨, 여기에 plan객체가 넘어가야 함
        */
        PlanItem planItem = PlanItem.get(params.id)
        [ planItem : planItem ]
    }
/*
    def show = {
        def planItemInstance = PlanItem.get( params.id )

        if(!planItemInstance) {
            flash.message = "PlanItem not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ planItemInstance : planItemInstance ] }
    }
*/
    def delete = {
        def planItemInstance = PlanItem.get( params.id )
        if(planItemInstance) {
            try {
                planItemInstance.delete()
                flash.message = "PlanItem ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "PlanItem ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "PlanItem not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def planItemInstance = PlanItem.get( params.id )

        if(!planItemInstance) {
            flash.message = "PlanItem not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ planItemInstance : planItemInstance ]
        }
    }

    def update = {
        def planItemInstance = PlanItem.get( params.id )
        if(planItemInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(planItemInstance.version > version) {
                    
                    planItemInstance.errors.rejectValue("version", "planItem.optimistic.locking.failure", "Another user has updated this PlanItem while you were editing.")
                    render(view:'edit',model:[planItemInstance:planItemInstance])
                    return
                }
            }
            planItemInstance.properties = params
            if(!planItemInstance.hasErrors() && planItemInstance.save()) {
                flash.message = "PlanItem ${params.id} updated"
                redirect(action:show,id:planItemInstance.id)
            }
            else {
                render(view:'edit',model:[planItemInstance:planItemInstance])
            }
        }
        else {
            flash.message = "PlanItem not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def planItem = new PlanItem()
        planItem.properties = params
        Plan plan = Plan.get(params.id)
        [ planItem : planItem , plan : plan]
    }
    def save = {
        
        if (!session.user){
            redirect(controller:'user', action:'login')
        }else{
            def planItem = new PlanItem(params)
            User user = User.find(session.user)

            //Plan plan = Plan.get(user.plans.get(user.plans.count()).id)
            Plan plan = Plan.get(params.planId)
            planItem.save()
            plan.addToItems(planItem)
            redirect(controller:'dashboard',action:'totalPlan')
        }
    }
/*
    def create = {
        def planItemInstance = new PlanItem()
        planItemInstance.properties = params
        return ['planItemInstance':planItemInstance]
    }

    def save = {
        def planItemInstance = new PlanItem(params)
        if(!planItemInstance.hasErrors() && planItemInstance.save()) {
            flash.message = "PlanItem ${planItemInstance.id} created"
            redirect(action:show,id:planItemInstance.id)
        }
        else {
            render(view:'create',model:[planItemInstance:planItemInstance])
        }
    }
*/
}
